using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.TFL.Base;
using Toenda.TFL.Data;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class ZappZoom : System.Web.UI.Page {
	private AccountProvider ap = new AccountProvider();
	private StaticProvider sp = new StaticProvider();
	private ImageProvider ip = new ImageProvider();
	private Tools t = new Tools();

	/// <summary>
	/// Page_PreInit
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_PreInit(object sender, EventArgs e) {
		HttpBrowserCapabilities hbc = Request.Browser;

		switch(hbc.Type) {
			case "IE":
			case "IE5":
			case "IE6":
			case "IE7":
				this.Page.Theme = "IE7";
				break;

			default:
				this.Page.Theme = "Default";
				break;
		}

		try {
			if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
				Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
				this.Culture = acc.Language;
				this.UICulture = acc.Language.Substring(0, 2);
			}
			else {
				string[] langs = Request.UserLanguages;

				switch(langs[0].ToLower()) {
					case "de":
					case "de-de":
						this.Culture = "de-DE";
						break;

					case "en":
					case "en-us":
					case "en-gb":
						this.Culture = "en-US";
						break;

					default:
						this.Culture = "en-US";
						break;
				}

				this.UICulture = langs[0].Substring(0, 2);
			}
		}
		catch(Exception ex) {
			string s = ex.Message;
			this.Culture = "en-US";
			this.UICulture = "en";
		}
	}

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		bool isLoggedIn = false;
		bool showImage = true;
		Account tmp = new Account();

		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			tmp = ap.GetAccountBySession(Session["Zappr"].ToString());
			isLoggedIn = true;
		}

		// read config
		XmlProvider xml = new XmlProvider(Server.MapPath("Configuration/Configuration.xml"));

		Control ctrl = Page.Master.FindControl("plcMasterVersion");
		ctrl.Controls.Add(new LiteralControl("<!--"));
		ctrl.Controls.Add(new LiteralControl("\n\tPowered by ASP.NET on Rails " + t.GetAssemblyVersion()));
		ctrl.Controls.Add(new LiteralControl("\n\tPowered by Toenda Foundation Library " + AssemblyInfo.Version));
		ctrl.Controls.Add(new LiteralControl("\n\tPowered by ASP.NET Ajax Extension"));
		ctrl.Controls.Add(new LiteralControl("\n\t-->"));

		plcHeader.Controls.Add(LoadControl("Controls/ctrl_header.ascx"));
		plcFooter.Controls.Add(LoadControl("Controls/ctrl_footer.ascx"));

		/*
		 * load image
		 */

		string size = "";

		if(Request.QueryString.Get("size") != null) {
			size = Request.QueryString.Get("size").ToString();
		}

		bool letView = false;

		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			isLoggedIn = true;
		}

		if(Request.QueryString.Get("item") != null) {
			ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("item")));
			Account acc = ap.GetAccount(item.UserID);

			base.Title = xml.ReadTag("WebPageTitleMain");
			base.Title += " &raquo; ";
			base.Title += item.Name;

			// check privacy
			if(isLoggedIn && item.UserID == tmp.ID) {
				if(item.Access > 0) {
					// this time: only the user itself
					// next time: friends and family
					if(item.UserID == acc.ID)
						letView = true;
				}
				else {
					letView = true;
				}
			}
			else {
				if(item.Access == 0) {
					letView = true;
				}
			}

			/*
			 * image
			 */

			if(item.Licence == "cl") {
				showImage = false;

				if(item.UserID == tmp.ID) {
					showImage = true;
				}
			}
			else {
				showImage = true;
			}

			if(showImage) {
				if(letView) {
					/*
					 * get image size
					 */

					string url = "";
					string path = "";

					switch(size) {
						case "o":
							url = "~/Images/" + item.ImageOriginal;
							path = "Images/" + item.ImageOriginal;
							break;

						case "sq":
							url = "~/Images/" + item.Image75x75;
							path = "Images/" + item.Image75x75;
							break;

						case "t":
							url = "~/Images/" + item.Image100;
							path = "Images/" + item.Image100;
							break;

						case "s":
							url = "~/Images/" + item.Image240;
							path = "Images/" + item.Image240;
							break;

						case "m":
							url = "~/Images/" + item.Image500;
							path = "Images/" + item.Image500;
							break;

						case "l":
							url = "~/Images/" + item.Image1024;
							path = "Images/" + item.Image1024;
							break;

						default:
							if(item.Image1024 != null && item.Image1024 != "") {
								url = "~/Images/" + item.Image1024;
								path = "Images/" + item.Image1024;
								size = "l";
							}
							else {
								url = "~/Images/" + item.ImageOriginal;
								path = "Images/" + item.ImageOriginal;
								size = "o";
							}
							break;
					}

					/*
					 * head
					 */

					HyperLink linkback = new HyperLink();
					linkback.CssClass = "zappZoomLink";
					linkback.Text = "&laquo; " + item.Name;
					//linkback.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
					linkback.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";

					plcZappr.Controls.Add(linkback);

					Label uploadedon = new Label();
					uploadedon.CssClass = "listText";
					uploadedon.Text = GetLocalResourceObject("UploadedOn").ToString();
					uploadedon.Text += " ";
					uploadedon.Text += item.UploadDate.ToShortDateString();
					uploadedon.Text += " ";
					uploadedon.Text += GetLocalResourceObject("By").ToString();
					uploadedon.Text += " ";

					plcUser.Controls.Add(uploadedon);

					HyperLink user = new HyperLink();
					user.CssClass = "listText";
					user.Text = acc.Username;
					//user.NavigateUrl = "~/default.aspx?id=images&user=" + acc.Username;
					user.NavigateUrl = "~/images/" + acc.Username + "/default.aspx";

					plcUser.Controls.Add(user);

					/*
					 * ZappActions
					 */

					Label imagesize = new Label();
					imagesize.CssClass = "zappZoomText"; //txtH2
					imagesize.Text = GetLocalResourceObject("ImageSize").ToString();
					imagesize.Text += ": ";

					plcZappActions.Controls.Add(imagesize);

					// sq
					HyperLink linksq = new HyperLink();
					linksq.CssClass = (size == "sq" ? "zappZoomActive" : "zappZoomLink");
					linksq.Text = GetLocalResourceObject("Square").ToString();
					linksq.NavigateUrl = "~/zappzoom.aspx?item=" + item.ImageID.ToString() + "&size=sq";

					plcZappActions.Controls.Add(linksq);

					// t
					if(item.Image100 != null && item.Image100 != "") {
						// first slash
						Label litSlashOne = new Label();
						litSlashOne.CssClass = "zappZoomText";
						litSlashOne.Text = " / ";

						plcZappActions.Controls.Add(litSlashOne);

						HyperLink linkt = new HyperLink();
						linkt.CssClass = (size == "t" ? "zappZoomActive" : "zappZoomLink");
						linkt.Text = GetLocalResourceObject("Thumbnail").ToString();
						linkt.NavigateUrl = "~/zappzoom.aspx?item=" + item.ImageID.ToString() + "&size=t";

						plcZappActions.Controls.Add(linkt);
					}

					// s
					if(item.Image240 != null && item.Image240 != "") {
						// second slash
						Label litSlashTwo = new Label();
						litSlashTwo.CssClass = "zappZoomText";
						litSlashTwo.Text = " / ";

						plcZappActions.Controls.Add(litSlashTwo);

						HyperLink links = new HyperLink();
						links.CssClass = (size == "s" ? "zappZoomActive" : "zappZoomLink");
						links.Text = GetLocalResourceObject("Small").ToString();
						links.NavigateUrl = "~/zappzoom.aspx?item=" + item.ImageID.ToString() + "&size=s";

						plcZappActions.Controls.Add(links);
					}

					// m
					if(item.Image500 != null && item.Image500 != "") {
						// third slash
						Label litSlashThree = new Label();
						litSlashThree.CssClass = "zappZoomText";
						litSlashThree.Text = " / ";

						plcZappActions.Controls.Add(litSlashThree);

						HyperLink linkm = new HyperLink();
						linkm.CssClass = (size == "m" ? "zappZoomActive" : "zappZoomLink");
						linkm.Text = GetLocalResourceObject("Medium").ToString();
						linkm.NavigateUrl = "~/zappzoom.aspx?item=" + item.ImageID.ToString() + "&size=m";

						plcZappActions.Controls.Add(linkm);
					}

					// l
					if(item.Image1024 != null && item.Image1024 != "") {
						// fourth slash
						Label litSlashFour = new Label();
						litSlashFour.CssClass = "zappZoomText";
						litSlashFour.Text = " / ";

						plcZappActions.Controls.Add(litSlashFour);

						HyperLink linkl = new HyperLink();
						linkl.CssClass = (size == "l" ? "zappZoomActive" : "zappZoomLink");
						linkl.Text = GetLocalResourceObject("Large").ToString();
						linkl.NavigateUrl = "~/zappzoom.aspx?item=" + item.ImageID.ToString() + "&size=l";

						plcZappActions.Controls.Add(linkl);
					}


					// fivth slash
					Label litSlashFive = new Label();
					litSlashFive.CssClass = "zappZoomText";
					litSlashFive.Text = " / ";

					plcZappActions.Controls.Add(litSlashFive);

					HyperLink linko = new HyperLink();
					linko.CssClass = (size == "o" ? "zappZoomActive" : "zappZoomLink");
					linko.Text = GetLocalResourceObject("Original").ToString();
					linko.NavigateUrl = "~/zappzoom.aspx?item=" + item.ImageID.ToString() + "&size=o";

					plcZappActions.Controls.Add(linko);

					/*
					 * head 2
					 */

					Image imgtag = new Image();
					imgtag.ImageUrl = "~/App_Themes/Default/SilkIcons/save.png";

					plcSave.Controls.Add(imgtag);

					Label space0 = new Label();
					space0.Text = " ";

					plcSave.Controls.Add(space0);

					HyperLink down = new HyperLink();
					down.CssClass = "zappZoomLink";
					down.Text = GetLocalResourceObject("Download").ToString();
					down.NavigateUrl = url;

					plcSave.Controls.Add(down);

					/*
					 * image
					 */

					imgView.ImageUrl = url;

					/*
					 * licence
					 */
					if(acc.Type != 0) {
						Static st = sp.GetStaticItem("L001", item.Licence);

						Label liclabel = new Label();
						liclabel.CssClass = "listText";
						liclabel.Text = GetLocalResourceObject("Copyright").ToString();
						liclabel.Text += " ";
						liclabel.Text += acc.Username;

						if(acc.Firstname != "" || acc.Lastname != "") {
							liclabel.Text += " (";
							liclabel.Text += acc.Firstname;
							liclabel.Text += " ";
							liclabel.Text += acc.Lastname;
							liclabel.Text += ")";
						}

						plcLicence.Controls.Add(liclabel);

						// license
						if(item.Licence == "cl") {
							Label licd = new Label();
							licd.CssClass = "listText";
							licd.Text = st.Content;

							plcLicence.Controls.Add(new LiteralControl("<span class=\"listText\"> (</span>"));
							plcLicence.Controls.Add(licd);
							plcLicence.Controls.Add(new LiteralControl("<span class=\"listText\">)</span>"));
						}
						else {
							if(st.Content != "" 
							&& item.Licence != ""
							&& item.Licence != null
							&& item.Licence != "--") {
								HyperLink lic = new HyperLink();
								lic.CssClass = "listText";
								lic.Text = st.Content;

								// eventuell auswahl des links anhand der cc lizens
								//lic.NavigateUrl = "http://creativecommons.org/licenses/by-nc-sa/3.0/";
								lic.NavigateUrl = "http://creativecommons.org/licenses/";

								plcLicence.Controls.Add(
									new LiteralControl("<span class=\"listText\"> (</span>")
								);
								
								plcLicence.Controls.Add(lic);
								
								plcLicence.Controls.Add(
									new LiteralControl("<span class=\"listText\">)</span>")
								);
							}
						}

						plcLicence.Controls.Add(new LiteralControl("<br />"));
						plcLicence.Controls.Add(new LiteralControl("<br />"));
					}
					else {
						Label liclbl = new Label();
						liclbl.CssClass = "listText";
						liclbl.Text = GetLocalResourceObject("Copyright").ToString();
						liclbl.Text += " ";
						liclbl.Text += acc.Username;

						if(acc.Firstname != "" || acc.Lastname != "") {
							liclbl.Text += " (";
							liclbl.Text += acc.Firstname;
							liclbl.Text += " ";
							liclbl.Text += acc.Lastname;
							liclbl.Text += ")";
						}

						plcLicence.Controls.Add(liclbl);
						plcLicence.Controls.Add(new LiteralControl("<br />"));
						plcLicence.Controls.Add(new LiteralControl("<br />"));
					}

					/*
					 * html code
					 */

					string imagewidth = "";
					string imageheight = "";

					System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Server.MapPath(path));
					imagewidth = bmp.Size.Width.ToString();
					imageheight = bmp.Size.Height.ToString();

					txtPath.Attributes.Add("onfocus", "this.select();");
					txtPath.Text = "http://www.zappr.net/" + path;

					txtHtml.Attributes.Add("onfocus", "this.select();");
					txtHtml.Text = "<div style=\"width:" + imagewidth + "px; text-align:right;\">";
					
					//txtHtml.Text += "<a href=\"http://www.zappr.net/default.aspx?item=";
					//txtHtml.Text += item.ImageID.ToString();
					txtHtml.Text += "<a href=\"http://www.zappr.net/photo/";
					txtHtml.Text += item.ImageID.ToString();
					txtHtml.Text += "/default.aspx";

					txtHtml.Text += "\" title=\"" + base.Title + "\">";
					txtHtml.Text += "<img src=\"http://www.zappr.net/" + path + "\" ";
					txtHtml.Text += "width=\"" + imagewidth + "\" height=\"" + imageheight + "\" ";
					txtHtml.Text += "alt=\"" + item.Name + "\" border=\"0\" ";
					txtHtml.Text += "style=\"border:1px solid #777;\" />";
					txtHtml.Text += "</a>";
					txtHtml.Text += "<span style=\"float:left;font-size: 11px;\">" + item.Name + "</span>";
					txtHtml.Text += "<br />";
					txtHtml.Text += "<span style=\"font-size: 11px;\">";
					txtHtml.Text += "Hosted on ";
					txtHtml.Text += "<a style=\"text-decoration: none !important;\" ";
					txtHtml.Text += "href=\"http://www.zappr.net\" target=\"_blank\"><strong>";
					txtHtml.Text += "<span style=\"color:#222222;\">Zapp</span>";
					txtHtml.Text += "<span style=\"color:#339933;\">r</span>";
					txtHtml.Text += "</strong>";
					txtHtml.Text += "</span>";
					txtHtml.Text += "</a>";
					txtHtml.Text += "</div>";
				}
			}
			else {
				Response.Redirect("~/default.aspx?item=" + Request.QueryString.Get("item").ToString());
			}
		}
	}

	/// <summary>
	/// down_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void down_Click(object sender, CommandEventArgs e) {
		Response.Redirect(e.CommandArgument.ToString());
	}
}
